package com.rabbiter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.common.QueryPageParam;
import com.rabbiter.common.Result;
import com.rabbiter.entity.Provider;
import com.rabbiter.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    /*
     * 新增供应商
     */
    @PostMapping("/save")
    public Result save(@RequestBody Provider Provider){
        return providerService.save(Provider)?Result.success():Result.fail();
    }

    /*
     * 更新供应商
     */
    @PostMapping("/update")
    public Result update(@RequestBody Provider Provider){
        return providerService.updateById(Provider)?Result.success():Result.fail();
    }

    /*
     * 删除供应商
     */
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return providerService.removeById(id)?Result.success():Result.fail();
    }

    /*
     * 查询供应商列表
     */
    @GetMapping("/list")
    public Result list(){
        List list = providerService.list();
        return Result.success(list);
    }

    /*
     * 模糊查询：根据输入查询供应商并以分页的形式展示
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<Provider> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Provider> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            queryWrapper.like(Provider::getName,name);
        }

        IPage result = providerService.pageCC(page,queryWrapper);
        ArrayList<Object> list = new ArrayList<>();
        list.add(result.getRecords());
        list.add(result.getTotal());
        return Result.success(list);
    }

}
